Skip to content

Fix #15193 Wrong type for hex literals that do not fit in 32-bit long - #9026

Open
wjakobsson wants to merge 3 commits into
cppcheck-opensource:mainfrom
wjakobsson:hex-literal-type
Open

wjakobsson wants to merge 3 commits into
cppcheck-opensource:mainfrom
wjakobsson:hex-literal-type

Conversation

@wjakobsson

Copy link
Copy Markdown
Collaborator

No description provided.

@wjakobsson wjakobsson changed the title #15193 Wrong type for hex literals that do not fit in 32-bit long Fix #15193 Wrong type for hex literals that do not fit in 32-bit long Nov 13, 2026

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no problems in the change. Switching from value >> 2 to value >> 1 makes the "fits in unsigned int/long" check exact. For a biguint, value >> 1 <= INT_MAX is the same as value <= UINT_MAX. On unix32, unix64 and win64 every hex and binary literal I tried now gets the type the C standard gives it. The U and L suffixes work too.

One case is still wrong, and the PR does not cause it. MathLib::isDec() counts octal literals as decimal. So on unix32 037777777777 (= 0xFFFFFFFF) gets the type signed long long instead of unsigned int, in both base and PR. The bug is in the same !MathLib::isDec(tokStr) condition the PR edits, so it could be fixed here or in a separate ticket.

Testing

  • Built cppcheck and testrunner in pr/, and cppcheck in base/. Both built cleanly.
  • ./testrunner TestSymbolDatabase TestValueFlow TestType TestOther TestCondition: 863 tests, 0 failed.
  • Compared the valueType of literals in --dump output between base and PR on unix32, unix64 and win64, using:
    void f(void) {
        (void)0xFFFFFFFF; (void)0x1FFFFFFFF; (void)0x100000000; (void)0xFFFFFFFFU; (void)0x100000000U;
        (void)037777777777; (void)077777777777; (void)0x8000000000000000; (void)0xFFFFFFFFFFFFFFFFL;
        (void)0x100000000L; (void)0x80000000L; (void)0x8000000000000000L;
    }
    • Base gave wrong types, for example 0x100000000 → unsigned int and 0x100000000L → unsigned long on unix32.
    • The PR gives the right types: signed long long on unix32/win64 and signed long on unix64. 0x100000000U → unsigned long long/unsigned long. 0xFFFFFFFF stays unsigned int, and 0x80000000L stays unsigned long on 32-bit long platforms.
    • Binary literals are also right, for example 0b1…1 (32 ones) → unsigned int.
  • Older bug found again, the same in base and PR: octal 037777777777 → signed long long on unix32. It should be unsigned int, because MathLib::isDec treats octal as decimal.
  • Older bug found again, the same in base and PR: an out-of-range literal 0x10000000000000000 gives internalError from MathLib::toBigUNumber.

Automated review of e9f7fde by Claude. The pull request was built and tested locally.

Comment thread lib/symboldatabase.cpp Resolved
@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit e9f7fde (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9026/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: -1.9% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9026/2026-11-13_14-22-25-e9f7fde2ec4d_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit f23df7d (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9026/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: +0.4% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9026/2026-11-17_13-34-13-f23df7d898db_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no new problems. The latest commit fixes the octal issue raised in C2. isDecimal = isDec && !isOct now sends octal literals down the same unsigned path as hex and binary literals. With the >> 1 checks, literals that fit in unsigned int or unsigned long get the type the C standard gives them on unix32, unix64 and win64. The new unit tests cover hex, octal and the unsigned/long suffixes.

Comments

# Comment By Where Status
1 Octal literals treated as decimal by MathLib::isDec, so they never get unsigned types AI review lib/symboldatabase.cpp:8013 ✅ Fixed

Testing

  • Built cppcheck and testrunner in pr/. The build finished with no errors.

  • ./testrunner TestSymbolDatabase: 430 tests, 0 failed.

  • TestValueFlow, TestType, TestOther, TestCondition, Test64BitPortability and TestIO: 0 failed.

  • Checked the valueType in the --dump output on unix32, unix64 and win64 for these literals:

    • 037777777777, with no suffix and with U and L
    • 040000000000 and 017777777777
    • 0x1'0000'0000 and 0'37777777777 (C++14 digit separators)
    • 0, 00 and 0U
    • 2147483648 and 4294967295
    • 01777777777777777777777 (2^64-1) and 0777777777777777777777
    • a binary literal of 32 ones
    • -037777777777

    All of them got the types the standard gives them. For example, 037777777777 → unsigned int on every platform. 037777777777L → unsigned long on unix32/win64 and signed long on unix64. Octal 2^64-1 → unsigned long long on unix32/win64 and unsigned long on unix64. Decimal literals still never become unsigned.


Automated review of f23df7d by Claude. The pull request was built and tested locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants